package com.wsc.services.controller;

import com.wsc.services.util.VelocityUtil;
import com.wsc.tools.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 模板控制层
 */
@RestController
@RequestMapping("/template")
public class TemplateController {


    VelocityUtil velocityUtil = new VelocityUtil();

    /**
     * 获取字符
     * @param path 地址
     * @param key 值
     * @param model 数据
     * @return 生成的模板字符串
     * @throws Exception
     */
    @PostMapping("/get")
    public String getTemplateString(@RequestParam("path") String path,
                                    @RequestParam("key") String key,
                                    @RequestBody Object model) throws Exception {

            return velocityUtil.getTemplateString(path,key,model);

    }
    /**
     * 获取字符
     * @param path 地址
     *  @param toPath 要写入的地址
     * @param key 值
     * @param model 数据
     * @return 生成的模板字符串
     * @throws Exception
     */
    @PostMapping("/write")
    public AjaxResult writeTemplate(@RequestParam("path")String path,
                                    @RequestParam("toPath")String toPath,
                                    @RequestParam("key") String key,
                                    @RequestBody Object model) {
        try {
            velocityUtil.writeTemplate(path,toPath,key,model);
            return AjaxResult.getInstance().setMsg("写入成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getInstance().setCode(500).setMsg("写入失败");
        }


    }

}
